//友好时间格式
Date.prototype.friendTime = function () {
  const d = this;
  const now = Date.now();
  const diff = (now - d) / 1000;
  if (diff < 30) {
    //30秒以前
    return "刚刚";
  } else if (diff < 3600) {
    //60分钟以前
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    //24小时前
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    //2天前
    return "1天前";
  } else {
    //超过2天显示年日
    return `${
      d.getFullYear() + 1
    }年${d.getMonth()}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`;
  }
};

//上次登录时间
Date.prototype.normalTime = function () {
  const d = this;
  const now = Date.now();
  return `${d.getFullYear()}年-${
    d.getMonth() + 1
  }月${d.getDate()}日-${d.getHours()}:${d.getMinutes()}`;
};

//生日年月日
Date.prototype.birthdayTime = function () {
  const time = this;
  var y = time.getFullYear(); //获取年
  var mo = time.getMonth(); //获取月
  mo = parseInt(mo < 10 ? "0" + mo : mo) + 1;
  var d = time.getDate(); //获取天
  d = d < 10 ? "0" + d : d;
  return `${y}年${mo}月${d}日`;
};

//开始时间
Date.prototype.concessionalRate = function () {
  const time = this;
  var y = time.getFullYear(); //获取年
  var mo = time.getMonth(); //获取月
  mo = parseInt(mo < 10 ? "0" + mo : mo) + 1;
  var d = time.getDate(); //获取天
  var h = time.getHours() >= 10 ? time.getHours() : "0" + time.getHours(); //获取时
  var m = time.getMinutes() >= 10 ? time.getMinutes() : "0" + time.getMinutes(); //获取分
  var s = time.getSeconds() >= 10 ? time.getSeconds() : "0" + time.getSeconds(); //获取秒
  d = d < 10 ? "0" + d : d;
  return `${y}-${mo}-${d} ${h}:${m}:${s}`;
};

//结束时间
Date.prototype.Rate = function () {
  const time = this;
  var y = time.getFullYear(); //获取年
  var mo = time.getMonth(); //获取月
  mo = parseInt(mo < 10 ? "0" + mo : mo) + 1;
  var d = time.getDate(); //获取天
  var h = time.getHours() >= 10 ? time.getHours() : "0" + time.getHours(); //获取时
  var m = time.getMinutes() >= 10 ? time.getMinutes() : "0" + time.getMinutes(); //获取分
  var s = time.getSeconds() >= 10 ? time.getSeconds() : "0" + time.getSeconds(); //获取秒
  d = d < 10 ? "0" + d : d;
  return `${y}-${mo}-${d} ${h}:${m}:${s}`;
};

//创建时间
Date.prototype.creationTime = function () {
  const d = this;
  const now = Date.now();
  const diff = (now - d) / 1000;
  if (diff < 30) {
    //30秒以前
    return "刚刚";
  } else if (diff < 3600) {
    //60分钟以前
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    //24小时前
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    //2天前
    return "1天前";
  } else {
    //超过2天显示年日
    return `${d.getFullYear()}年${d.getMonth()}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`;
  }
};


// 时间
Date.prototype.findFriendTime = function (time = this) {
  // 当time的值为string时将其转换
  if (typeof time === "string") time = new Date(time); // 获取现在时间
  let nowTime = new Date(); // 获取时间差(相差秒)
  let diff = (nowTime - time) / 1000;

  if (diff < 30) return "刚刚";
  else if (diff < 60 * 60) return Math.ceil(diff / 60) + "分钟";
  else if (diff < 24 * 60 * 60) return Math.floor(diff / 60 / 60) + "小时";
  else if (diff < 24 * 60 * 60 * 2) return Math.floor(diff / 60 / 60) + "小时";

  return new Date(diff).format("yyyy年MM月dd日qq分hh");
};

// 时间格式化
Date.prototype.format = function (format: string) {
  var o = {
    "M+": this.getMonth() + 1, //月
    "d+": this.getDate(), //日
    "h+": this.getHours(), //时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
  };
  if (/(y+)/.test(format))
    format = format.replace(
      RegExp.$1,
      (this.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  for (var k in o)
    if (new RegExp("(" + k + ")").test(format))
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
  return format;
};
